/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.aroundme.model.Place;
import org.aroundme.model.Search;
import org.aroundme.persistency.PlaceManager;
import org.aroundme.utility.Constants;
import org.aroundme.utility.SearchTokenizer;

/**
 * This class is the controller for performing searches
 * 
 */
public class SearchController {
	private SearchTokenizer searchTokenizer = null;
	private PlaceManager placeManager = null;
	private ResponseController responseController = null;

	public SearchController() {
	}

	/**
	 * executes a search
	 * 
	 * @param search
	 *            search query
	 * @param begin
	 *            start-index of the data to be retrieved
	 * @param dataAmount
	 *            max-amount of data to be retrieved
	 * @param cellid
	 *            id of the cell
	 * @param includeGlobal
	 *            if global places should be included in the search
	 * @throws IOException
	 */
	public void search(String search, int begin, int dataAmount, String cellid) throws IOException {
		Search s = searchTokenizer.createSearchObject(search);

		List<Place> places = new ArrayList<Place>();
		places.addAll(placeManager.searchPlaces(s, begin, dataAmount));

		// TODO comment collection should be reversed
		/*
		 * Remove all places where the according location doesn't match the
		 * cell-id. This could be possibly done with a Hibernate criteria
		 */
		for (int i = 0; i < places.size(); i++) {
			Place tmp = places.get(i);
			if (!tmp.getLocation().getCellid().equals(cellid)) {
				places.remove(i);
			}
		}

		if (places != null) {
			responseController.sendPlaces(places);
		} else {
			responseController.sendAlert(Constants.ALERT_ERROR,
					"No results found!");
		}
	}

	/*
	 * Getters and Setters
	 */

	public SearchTokenizer getSearchTokenizer() {
		return searchTokenizer;
	}

	public void setSearchTokenizer(SearchTokenizer searchTokenizer) {
		this.searchTokenizer = searchTokenizer;
	}

	public PlaceManager getPlaceManager() {
		return placeManager;
	}

	public void setPlaceManager(PlaceManager placeManager) {
		this.placeManager = placeManager;
	}

	public ResponseController getResponseController() {
		return responseController;
	}

	public void setResponseController(ResponseController responseController) {
		this.responseController = responseController;
	}

}
